package com.xinxing.learning.batch.util;

import cn.hutool.core.date.DateUtil;
import com.xinxing.learning.batch.common.SyncConstants;
import com.xinxing.learning.batch.entity.CdcTemp;
import com.xinxing.learning.batch.entity.User;
import com.xinxing.learning.batch.service.CommonService;
import com.xinxing.learning.batch.service.batch.JobLauncherService;
import com.xxl.job.core.context.XxlJobHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.*;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;

import java.util.Map;

@Slf4j
public class JobUtil {

    /**
     * 传递参数:
     * https://blog.csdn.net/weixin_45357522/article/details/110651906
     * https://blog.csdn.net/lewenson/article/details/120179510
     *
     * @return
     */
    public static JobParameters makeJobParameters() {
        User user = new User();
        user.setPhone("13678900987");
        user.setTitle("test");
        user.setName("张三");
        user.setEmail("test@qq.com");
        JobParameters jobParameters = new JobParametersBuilder()
                .addLong("time", System.currentTimeMillis())
                .addString("user", user.toString())
                .toJobParameters();

        return jobParameters;
    }

    public static JobParameters makeJobParameters(CdcTemp currentCdcTemp) {
        String lastUpdateTime = DateUtil.formatDateTime(currentCdcTemp.getLastUpdateTime());
        JobParameters jobParameters = new JobParametersBuilder()
                .addLong("time", System.currentTimeMillis())
                .addString(SyncConstants.STR_LAST_UPDATE_TIME, lastUpdateTime)
                .toJobParameters();
        return jobParameters;
    }

    /**
     * 执行xxl的任务
     *
     * @param jobName
     * @param commonService
     * @param jobLauncherService
     * @param job
     * @return
     * @throws JobParametersInvalidException
     * @throws JobExecutionAlreadyRunningException
     * @throws JobRestartException
     * @throws JobInstanceAlreadyCompleteException
     */
    public static void runJob4Executor(String jobName, CommonService commonService
            , JobLauncherService jobLauncherService, Job job) throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, JobRestartException {
        XxlJobHelper.log("start sync " + jobName + " data");
        JobParameters jobParameters = commonService.initJobParam();
        Map<String, Object> resultMap = jobLauncherService.startJob(job, jobParameters);
        String resultStr = (String) resultMap.get(SyncConstants.STR_RETURN_RESULT);
        XxlJobHelper.log("end sync " + jobName + " data, result: " + System.lineSeparator() + resultStr);

        ExitStatus exitStatus = (ExitStatus) resultMap.get(SyncConstants.STR_RETURN_EXITSTATUS);
        String exitCode = exitStatus.getExitCode();
        //若成功，返回SUCCESS
        if (ExitStatus.COMPLETED.getExitCode().equals(exitCode)) {
            XxlJobHelper.handleSuccess();
        } else {
            XxlJobHelper.handleFail();
        }
    }
}
